/**
 * 功能描述  导航
 */
define(function (require, exports, module) {
    var Subnav  = require("subnav");
    var aw = require("ajaxwrapper");
	var user = require("store").get("user");
    
    var instance = {
        initComponent: function (params,widget) {
        	var subnav = new Subnav({
        		parentNode:".J-subnav",
        		model:{
        			title:"房间配置落实",
        			items : [{
						id:"search",
						type: "search",
						placeholder: "组团单位",
						handler: function(str){
							aw.ajax({
								url: "api/checkinimplement/search*",
								data: {
									s: str,
									properties:"groupReserve.planItemGroup.servicePoint.name," +
									"groupReserve.planItemGroup.allianceOrg.name," +
									"nonMemGroupReserve.nonMemberGroup.groupUnit",
									fetchProperties:"pkCIImplement,version,roomStatus.*,status.*," +
									"dataSource.*," + 
									"nonMemGroupReserve.*,nonMemGroupReserve.nonMemberGroup," + 
									"nonMemGroupReserve.nonMemberGroup.servicePoint.name," + 
									"nonMemGroupReserve.nonMemberGroup.*," + 
									"nonMemGroupReserve.nonMemberGroup.signUps.version," + 
									"nonMemGroupReserve.nonMemberGroup.operator.servicePoint.name," + 
									"groupReserve.planItemGroup.allianceOrg.name," +
									"groupReserve.signUpNumber," +
									"groupReserve.checkInDate," +
									"groupReserve.checkOutDate," +
									"groupReserve.planItemGroup.planItem.destination.name," +
									"groupReserve.planItemGroup.signUps.version," +
									"groupReserve.roomRegisters.pkRoomRegister," +
									"groupReserve.roomRegisters.room.code," +
									"groupReserve.roomRegisters.room.orgBuilding.name," +
									"groupReserve.planItemGroup.servicePoint.pkServicePoint," +
									"groupReserve.planItemGroup.servicePoint.name," +
									"checkInCar.pkCheckInCar,checkInCar.version,checkInCar.haveCar," +
									"checkInCar.address,checkInCar.status,checkInCar.description," +
									"checkInCar.address,checkInCar.carDate," +
									"checkInDinner.pkCheckInDinner,checkInDinner.version," + 
									"checkInDinner.haveDinner,checkInDinner.breakfast," +
									"checkInDinner.lunch,checkInDinner.supper," +
									"checkInDinner.status,checkInDinner.description"
								},
								dataType:"json",
								success: function(data){
									widget.get("roomBaseGrid").setData(data);
								}
							});
						}
					},{
        				id: "servicePoint",
        				type: "buttongroup",
        				all :{
        					show:false,
        					first:false
        				},
        				tip :"服务点",
        				keyField: "pkServicePoint",
        				valueField: "name",
//        				url: "api/servicepoint/operableservicepoints",
        				items:user.privateServicePoints,
        				params:function(){
        					return{
        						fetchProperties: "pkServicePoint,name"
        					}
        				},
//        				lazy: true,
        				handler : function(key,element){
        					widget.get("roomBaseGrid").refresh();
        				}
        			},{
        				id: "status",
        				tip:"状态",
        				type:"buttongroup",
        				items:[{
        					key: "Pending",
        					value: "待落实"
        				},{
        					key: "Pended",
        					value: "已落实"
        				},{
        					key: "Pending,Pended",
        					value: "全部"
        				}],
        				handler: function(){
        					widget.get("roomBaseGrid").refresh();
        				}
        			},{
        				id :"startDate",
        				tip: "入住日期",
        				type : "daterange",
        				ranges:{
        					"本年": [moment().startOf("year"), moment().endOf("days")]
        				},
        				handler : function(time){
        					widget.get("roomBaseGrid").refresh();
        				}
        			},{
        				id:"save",
        				type:"button",
        				text:"保存",
        				show:false,
        				handler:function(){
        					var cParams = {};
        					$(".J-grid-table tr").each(function(i,el){
        						var checkbox = $(el).find("input[type=checkbox]");
        						var id = checkbox.attr("id");
        						cParams[id] = checkbox.prop("checked");
        					});
        					cParams.pendDescription = $("#pendDescription").val();
        					var curData = widget.get("curData");
        					cParams.pkRoomRegister = curData.roomRegister.pkRoomRegister;
        					cParams.version = curData.roomRegister.version;
        					
        					var flag = true;
        					for(var key in cParams){
        						if(cParams[key] === false){
        							flag = false;
        						}
        					}
        					
        					if(flag){
        						cParams.status = "Pended";	
        					}else{
        						cParams.status = "Pending";
        					}
        					
        					aw.saveOrUpdate("api/roomregister/confirm",cParams,function(data){
	    	        			widget.get("grid").refresh();
	    						widget.show([".J-grid", ".J-form"]).hide([".J-detailgrid"]);
	    						widget.get("subnav").hide(["save"]);
    	        			});
        				}
        			},{
        				id:"return",
        				type:"button",
        				text:"返回",
        				show:false,
        				handler:function(){
        					var edit = widget.get("edit");
        					if(edit){
        						widget.set("edit", false);
        						widget.show([".J-form", ".J-grid"]).hide([".J-detailgrid"]);
        						widget.get("subnav").hide(["save"]).show([]);
        						widget.get("grid").refresh();
        					}else{
        						var dataSource = widget.get("dataSource");
        						if(dataSource == "GroupReserve"){
        							$(".J-grid-head-formel").show();
        						}
        						if(dataSource == "NonMember"){
        							$(".J-grid-head-formel").show();
        							$("th[data-name='show']").removeClass("hidden");
        							$(".J-grid-columns-show").removeClass("hidden");
        						}
        						widget.show([".J-checkgrid"]).hide([".J-form", ".J-roomtypegrid", ".J-grid", "J-detailgrid"]);
        						widget.get("grid").refresh();
        						widget.get("roomBaseGrid").refresh();
        						widget.get("subnav").hide(["return","save"]).show(["search","servicePoint","startDate","status"]);
        					}
        				}
        			}]
        		}
        	});
        	widget.set("subnav", subnav);
        }
    };
    module.exports = instance;
});
